package com.leo.foodmaster.admin.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.commons.annotation.Log;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.enums.BusinessType;
import com.leo.foodmaster.commons.util.ExcelUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.leo.foodmaster.admin.system.entity.SysLoginLogEntity;
import com.leo.foodmaster.admin.system.service.ISysLoginLogService;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 系统访问记录
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2021-12-22
 */
@RestController
@RequestMapping("/system/loginLog")
@Api(tags = "系统访问记录")
@Slf4j
public class SysLoginLogController {
    @Resource
    private ISysLoginLogService isysLoginLogService;

    @GetMapping("/pageList")
    @ApiOperation("分页")
    public Result<IPage<SysLoginLogEntity>> page(@ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
                                                 @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
                                                 @ApiParam(value = "用户名称") @RequestParam(required = false) String userName,
                                                 @ApiParam(value = "登录地址") @RequestParam(required = false) String ipaddr,
                                                 @ApiParam(value = "登录状态") @RequestParam(required = false) String status,
                                                 @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
                                                 @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime){
        //单表查询
    	IPage<SysLoginLogEntity> pageList = isysLoginLogService.pageList(new Page<>(pageNum, pageSize),userName, ipaddr, status, beginTime, endTime);
        return Result.success(pageList);
    }


    @DeleteMapping
    @ApiOperation("删除登录日志")
    @Log(title = "删除登录日志", businessType = BusinessType.DELETE)
    public Result<Object> delete(@RequestBody Integer[] ids){
        List<Integer> asList = Arrays.asList(ids);
        boolean remove = isysLoginLogService.removeByIds(asList);
        return Result.success(remove);
    }

    @DeleteMapping("/clean")
    @ApiOperation("清空登录日志")
    @Log(title = "清空登录日志", businessType = BusinessType.CLEAN)
    public Result<Object> clean(){
        boolean remove = isysLoginLogService.remove(Wrappers.lambdaUpdate(SysLoginLogEntity.class).setSql(true, "1=1"));
        return Result.success(remove);
    }

    @GetMapping("/export")
    @ApiOperation("导出登录日志数据")
    @Log(title = "导出登录日志", businessType = BusinessType.EXPORT)
    public Result<Object> export(@ApiParam(value = "用户名称") @RequestParam(required = false) String userName,
                                 @ApiParam(value = "登录地址") @RequestParam(required = false) String ipaddr,
                                 @ApiParam(value = "登录状态") @RequestParam(required = false) String status,
                                 @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
                                 @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime) {
        List<SysLoginLogEntity> list = isysLoginLogService.getList(userName, ipaddr, status, beginTime, endTime);
        ExcelUtil<SysLoginLogEntity> util = new ExcelUtil<>(SysLoginLogEntity.class);
        return util.exportExcel(list, "登录日志数据");
    }

}
